package com.hqj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqj.controller.vo.BlogVo;
import com.hqj.controller.vo.HomeAllVo;
import com.hqj.dao.BlogMapper;
import com.hqj.dao.CommentMapper;
import com.hqj.dao.FileListMapper;
import com.hqj.dao.ResourceMapper;
import com.hqj.entity.Blog;
import com.hqj.entity.Comment;
import com.hqj.entity.FileList;
import com.hqj.entity.ResourceEntity;
import com.hqj.service.BlogService;
import com.hqj.service.CommentService;
import com.hqj.service.FileListService;
import com.hqj.service.ResourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.geom.QuadCurve2D;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author 黄啟军
 * @version 1.0
 * @date 2020/5/16/016
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper,Blog> implements BlogService {

    @Resource
    private BlogMapper blogMapper;
//    @Resource
//    private BlogService blogService;
//    @Resource
//    private FileListService fileListService;
//    @Resource
//    private ResourceService resourceService;
    @Resource
     private FileListMapper fileListMapper;
    @Resource
    private ResourceMapper resourceMapper;
    @Resource
    private CommentMapper commentMapper;
    @Override
    public int likesAll(Long id) {
        Blog blog = blogMapper.selectById(id);
        blog.setLikes(blog.getLikes()+1);
        return blogMapper.updateById(blog);
    }

    @Override
    public List<Blog> essence(Integer current,Integer size) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.orderBy(true,true,"likes");
        return blogMapper.selectList(wrapper);
    }

    @Override
    public List<Blog> seekAll(String seek) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.like("title",seek)
        .or()
        .like("description",seek)
                .or().like("content",seek) ;
        return blogMapper.selectList(wrapper);
    }

    @Override
    public Blog findById(Long id) {
        Blog blog = blogMapper.selectById(id);
        blog.setWatch(blog.getWatch()+1);
        blogMapper.updateById(blog);
        return blog;
    }
    @Override
    public int updateByIds(Blog blog) {
        return blogMapper.updateById(blog);
    }

    @Override
    public List<Blog> findAll() {
        return blogMapper.selectList(null);
    }

    @Override
    public BlogVo pages(Integer current, Integer size) {
//        Page<Blog> page = blogService.page(new Page<>(current, size));
        Page<Blog> page = blogMapper.selectPage(new Page<>(current, size), null);
        BlogVo blogVo = new BlogVo();
        blogVo.setPages(page.getPages());
        blogVo.setTotal(page.getTotal());
        blogVo.setBlogList(page.getRecords());
        for(Blog blog:page.getRecords()){
            QueryWrapper<Comment> wrapper = new QueryWrapper<>();
            wrapper.eq("blog_id",blog.getId());
            List<Comment> comments = commentMapper.selectList(wrapper);
            blog.setDiscuss(comments.size());
            blogMapper.updateById(blog);
            System.out.println("==============================");
            System.out.println(blog);
        }
        return blogVo;
    }

    @Override
    public int saveSingle(Blog blog) {
        return blogMapper.insert(blog);
    }
    @Override
    public List<Object> homeAll() {
        List<Blog> list = blogMapper.selectList(null);
        List<Object> objectList = new ArrayList<>();
        HashSet<HomeAllVo> blogs = new HashSet<>();
        for(Blog blog:list){
            HomeAllVo allVo = new HomeAllVo();
            allVo.setClassifyCode(blog.getClassifyCode());
            allVo.setClassifyName(blog.getClassifyName());
            blogs.add(allVo);
        }
        QueryWrapper<FileList> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<FileList> lists = fileListMapper.selectList(wrapper);
        HashSet<HomeAllVo> fileLists = new HashSet<>();
        for(FileList fileList:lists){
            HomeAllVo allVo = new HomeAllVo();
            allVo.setClassifyCode(fileList.getId());
            allVo.setClassifyName(fileList.getName());
            fileLists.add(allVo);
        }
        QueryWrapper<ResourceEntity> wrapper3 = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<ResourceEntity> resourceEntities = resourceMapper.selectList(wrapper3);
        HashSet<HomeAllVo> homeAllVos = new HashSet<>();
        for(ResourceEntity resourceEntity:resourceEntities){
            HomeAllVo allVo = new HomeAllVo();
            allVo.setClassifyCode(resourceEntity.getId());
            allVo.setClassifyName(resourceEntity.getName());
            homeAllVos.add(allVo);
        }
        objectList.add(fileLists);
        objectList.add(blogs);
        objectList.add(homeAllVos);
        return objectList;
    }
}
